"""
Case Type   : 发布订阅
Case Name   : 创建发布订阅后，传输数据前订阅端减容/扩容
Create At   : 2022-11-08
Owner       : opentestcase026
Description :
    1.在两个集群创建表
    2.创建发布订阅
    3.减容订阅端备节点
    4.发布端插入数据
    5.订阅端查询数据是否更新
    6.扩容订阅端
    7.发布端更新数据
    8.订阅端查询数据是否更新
    9.订阅端主备切换
    10.发布端删除数据
    11.订阅端查询数据是否更新
    12.清理环境
Expect      :
    1.成功
    2.成功
    3.成功
    4.成功
    5.数据更新
    6.成功
    7.成功
    8.数据更新
    9.成功
    10.成功
    11.数据更新
    12.成功
History     :
    modified by opentestcase033 2023/10/10:设置wal_level参数添加dn_path
"""

import os
import time
import unittest

from yat.test import macro
from yat.test import Node
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger

Primary_SH = CommonSH('PrimaryDbUser')


@unittest.skipIf(3 != Primary_SH.get_node_num(), '非1+2环境不执行')
class Pubsubclass(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f"-----{os.path.basename(__file__)} start-----")
        self.pri_userdb_pub = Node(node='remote1_PrimaryDbUser')
        self.pri_userdb_sub = Node(node='PrimaryDbUser')
        self.std_userdb_sub1 = Node(node='Standby1DbUser')
        self.std_userdb_sub2 = Node(node='Standby2DbUser')
        self.constant = Constant()
        self.commsh_pub = CommonSH('remote1_PrimaryDbUser')
        self.commsh_sub = CommonSH('PrimaryDbUser')
        self.commsh_sub_sta1 = CommonSH('Standby1DbUser')
        self.commsh_sub_root = CommonSH('PrimaryRoot')
        self.com_pub = Common('remote1_PrimaryDbUser')
        self.com_sub = Common()
        self.table1 = 't_pubsub_0200_1'
        self.table2 = 't_pubsub_0200_2'
        self.subname = "sub_pubsub_0200"
        self.pubname = "pub_pubsub_0200"
        self.port = str(int(self.pri_userdb_pub.db_port) + 1)
        self.user_param_pub = f'-U {self.pri_userdb_pub.db_user} ' \
                              f'-W {self.pri_userdb_pub.db_password}'
        self.user_param_sub = f'-U {self.pri_userdb_sub.db_user} ' \
                              f'-W {self.pri_userdb_sub.db_password}'
        self.hostname = self.pri_userdb_sub.sh('hostname').result().strip()
        self.host_tuple = (self.pri_userdb_sub.ssh_host,
                           self.std_userdb_sub1.ssh_host,
                           self.std_userdb_sub2.ssh_host)
        self.params = {'-f': 'test_hosts'}
        self.wal_level = self.com_pub.show_param("wal_level",
                                            env_path=macro.DB_ENV_PATH_REMOTE1)
        self.enable_slot_log = self.com_sub.show_param("enable_slot_log")
        text = '-----查看集群状态，配置白名单，发布端设置wal_level=logical-----'
        self.log.info(text)
        self.log.info('-----发布端状态-----')
        status = self.commsh_pub.get_db_cluster_status(
            'detail', env_path=macro.DB_ENV_PATH_REMOTE1)
        self.log.info(status)
        self.assertEqual(status.count('Normal'), 4, f'执行失败:{text}')
        self.log.info('-----订阅端状态-----')
        status = self.commsh_sub.get_db_cluster_status('detail')
        self.log.info(status)
        self.assertEqual(status.count('Normal'), 4, f'执行失败:{text}')
        guc_res1 = self.commsh_pub.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG, '',
            'all', False, False, macro.DB_INSTANCE_PATH_REMOTE1,
            f'host    replication  {self.pri_userdb_sub.db_user}  '
            f'{self.pri_userdb_sub.db_host}/32  sha256',
            macro.DB_ENV_PATH_REMOTE1)
        self.log.info(guc_res1)
        guc_res2 = self.commsh_pub.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG, '',
            'all', False, False, macro.DB_INSTANCE_PATH_REMOTE1,
            f'host    replication  {self.std_userdb_sub1.db_user}  '
            f'{self.std_userdb_sub1.db_host}/32  sha256',
            macro.DB_ENV_PATH_REMOTE1)
        self.log.info(guc_res2)
        self.assertTrue(guc_res2, f'发布端配置白名单失败:{text}')
        if 'logical' != self.wal_level:
            result = self.commsh_pub.execute_gsguc(
                'set', self.constant.GSGUC_SUCCESS_MSG,
                'wal_level=logical', dn_path=macro.DB_INSTANCE_PATH_REMOTE1,
                env_path=macro.DB_ENV_PATH_REMOTE1)
            self.assertTrue(result, f'设置wal_level失败:{text}')
            restart_res = self.commsh_pub.restart_db_cluster(
                env_path=macro.DB_ENV_PATH_REMOTE1)
            self.assertTrue(restart_res, f'重启数据库失败:{text}')
        guc_res = self.commsh_sub.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG, '',
            'all', False, False, '',
            f'host    replication  {self.pri_userdb_pub.db_user} '
            f'{self.pri_userdb_pub.db_host}/32 sha256')
        self.log.info(guc_res)
        self.assertTrue(guc_res, '订阅端配置白名单失败:' + text)
        result = self.commsh_sub.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG,
            'enable_slot_log=on')
        self.log.info(result)
        self.assertTrue(result, f'开启逻辑复制槽主备同步失败:{text}')

    def test_pub_sub(self):
        text = "-----step1:在两个集群创建表;expect:成功-----"
        self.log.info(text)
        create_tb = f"drop table if exists {self.table1};" \
                    f"drop table if exists {self.table2};" \
                    f"create table {self.table1}(id1 int primary key," \
                    f"id2 int,id3 int) with (segment=on);" \
                    f"create table {self.table2}(id int primary key " \
                    f"constraint id_nn not null,use_filename varchar2(20)," \
                    f"filename varchar2(255),text varchar2(2000))" \
                    f"partition by range(id) (" \
                    f"partition p1 values less than(30)," \
                    f"partition P2 values less than(60)," \
                    f"partition P3 values less than(90)," \
                    f"partition P4 values less than(MAXVALUE));"
        pub_res = self.commsh_pub.execut_db_sql(
            create_tb, self.user_param_pub, None, macro.DB_ENV_PATH_REMOTE1)
        self.log.info(pub_res)
        self.assertEqual(pub_res.count(self.constant.DROP_TABLE_SUCCESS),
                         2, f'执行失败:{text}')
        self.assertEqual(pub_res.count(self.constant.TABLE_CREATE_SUCCESS),
                         4, f'执行失败:{text}')
        sub_res = self.commsh_sub.execut_db_sql(
            create_tb, sql_type=self.user_param_sub)
        self.log.info(sub_res)
        self.assertEqual(sub_res.count(self.constant.DROP_TABLE_SUCCESS),
                         2, f'执行失败:{text}')
        self.assertEqual(sub_res.count(self.constant.TABLE_CREATE_SUCCESS),
                         4, f'执行失败:{text}')

        text = "-----step2:创建发布订阅;expect:成功-----"
        self.log.info(text)
        create_pub = f"drop publication if exists {self.pubname};" \
                     f"create publication {self.pubname} for all tables;"
        pub_res = self.commsh_pub.execut_db_sql(
            create_pub, self.user_param_pub, None, macro.DB_ENV_PATH_REMOTE1)
        self.log.info(pub_res)
        self.assertIn(self.constant.drop_pub_succ_msg, pub_res,
                      f'执行失败:{text}')
        self.assertIn(self.constant.create_pub_succ_msg, pub_res,
                      f'执行失败:{text}')
        guc_res = self.commsh_sub.execute_generate(macro.COMMON_PASSWD)
        self.assertIn(self.constant.create_keycipher_success, guc_res,
                      f'执行失败:{text}')
        create_sub = f"drop subscription if exists {self.subname};" \
                     f"create subscription {self.subname} connection " \
                     f"'host={self.pri_userdb_pub.db_host} " \
                     f"port={self.port} user={self.pri_userdb_pub.db_user} " \
                     f"dbname={self.pri_userdb_pub.db_name} " \
                     f"password={self.pri_userdb_pub.ssh_password}' " \
                     f"publication {self.pubname} with (binary=false);"
        sub_res = self.commsh_sub.execut_db_sql(
            create_sub, sql_type=self.user_param_sub)
        self.log.info(sub_res)
        self.assertIn(self.constant.drop_sub_succ_msg, sub_res,
                      f'执行失败:{text}')
        self.assertIn(self.constant.create_sub_succ_msg, sub_res,
                      f'执行失败:{text}')

        try:
            text = "-----step3:减容订阅端备节点;expect:成功-----"
            self.log.info(text)
            drop_node_res = self.commsh_sub.exec_dropnode(self.std_userdb_sub1)
            self.log.info(drop_node_res)
            self.assertTrue(drop_node_res, f'执行失败:{text}')

            text = "-----step4:发布端插入数据;expect:成功-----"
            self.log.info(text)
            sql_cmd = f"insert into {self.table1} " \
                      f"values(generate_series(1,100),1,1);" \
                      f"insert into {self.table2} " \
                      f"values(generate_series(1,100),'first','%一',''); "
            insert_res = self.commsh_pub.execut_db_sql(
                sql_cmd, self.user_param_pub, None, macro.DB_ENV_PATH_REMOTE1)
            self.log.info(insert_res)
            self.assertEqual(insert_res.count(self.constant.INSERT_SUCCESS_MSG),
                             2, f'执行失败:{text}')

            text = "-----step5:订阅端查询数据是否更新;expect:数据更新-----"
            self.log.info(text)
            time.sleep(5)
            sql_select1 = f"select count(*) from {self.table1};" \
                          f"select count(*) from {self.table2};"
            sub_result = self.commsh_sub.execut_db_sql(
                sql_select1, self.user_param_sub)
            self.log.info(sub_result)
            self.assertEqual('100', str(sub_result.splitlines()[2]).strip(),
                             f'执行失败:{text}')
            self.assertEqual('100', str(sub_result.splitlines()[-2]).strip(),
                             f'执行失败:{text}')
        finally:
            text = "-----step6:扩容订阅端;expect:成功-----"
            self.log.info(text)
            sub_res1 = self.commsh_sub_root.exec_gs_sshexkey(
                macro.DB_SCRIPT_PATH, *self.host_tuple, **self.params)
            self.log.info(sub_res1)
            self.assertTrue(sub_res1, f'执行失败:{text}')
            sub_res2 = self.commsh_sub_root.exec_expension(
                f"{self.pri_userdb_sub.ssh_user}",
                f"{self.pri_userdb_sub.ssh_user}",
                f"{self.std_userdb_sub1.ssh_host}", f"{macro.DB_XML_PATH}")
            self.log.info(sub_res2)
            self.assertTrue(sub_res2, f'执行失败:{text}')

            text = "-----step7:发布端更新数据;expect:成功-----"
            self.log.info(text)
            update_sql = f"update {self.table1} set id2 = 999;" \
                         f"update {self.table2} set filename = 'new'; "
            update_res = self.commsh_pub.execut_db_sql(
                update_sql, self.user_param_pub,
                None, macro.DB_ENV_PATH_REMOTE1)
            self.log.info(update_res)
            self.assertEqual(update_res.count(self.constant.UPDATE_SUCCESS_MSG),
                             2, f'执行失败:{text}')

            text = "-----step8:订阅端查询数据是否更新;expect:数据更新-----"
            self.log.info(text)
            time.sleep(5)
            select_sql = f"select id2 from {self.table1};" \
                         f"select filename from {self.table2};"
            sub_result = self.commsh_sub.execut_db_sql(select_sql,
                                                       self.user_param_sub)
            self.log.info(sub_result)
            self.assertEqual(sub_result.count('999'), 100, f'执行失败:{text}')
            self.assertEqual(sub_result.count('new'), 100, f'执行失败:{text}')

            text = "-----step9:订阅端主备切换;expect:成功-----"
            self.log.info(text)
            sub_result = self.commsh_sub_sta1.execute_gsctl(
                'switchover', self.constant.SWITCH_SUCCESS_MSG)
            self.log.info(sub_result)
            self.assertTrue(sub_result, f'执行失败:{text}')
            sub_refresh_res = self.commsh_sub_sta1.exec_refresh_conf()
            self.log.info(sub_refresh_res)
            self.assertTrue(sub_refresh_res, f'执行失败:{text}')

            text = "-----step10:发布端删除数据;expect:成功-----"
            self.log.info(text)
            sql_cmd = f"delete from {self.table1};" \
                      f"delete from {self.table2}; "
            pub_result = self.commsh_pub.execut_db_sql(
                sql_cmd, self.user_param_pub, None, macro.DB_ENV_PATH_REMOTE1)
            self.log.info(pub_result)
            self.assertEqual(pub_result.count(self.constant.DELETE_SUCCESS_MSG),
                             2, f'执行失败:{text}')

            text = "-----step11:订阅端查询数据是否更新;expect:数据更新-----"
            self.log.info(text)
            sql = f"select count(*) from {self.table1};" \
                  f"select count(*) from {self.table2};"
            sub_res = self.commsh_sub_sta1.execut_db_sql(sql,
                                                         self.user_param_sub)
            self.log.info(sub_res)
            self.assertEqual('0', sub_res.splitlines()[2].strip(),
                             f'执行失败:{text}')
            self.assertEqual('0', sub_res.splitlines()[-2].strip(),
                             f'执行失败:{text}')

    def tearDown(self):
        text = "-----step12:清理环境;expect:成功-----"
        self.log.info(text)
        sub_switch_res = self.commsh_sub.execute_gsctl(
            'switchover', self.constant.SWITCH_SUCCESS_MSG)
        self.log.info(sub_switch_res)
        sub_refresh_res = self.commsh_sub.exec_refresh_conf()
        self.log.info(sub_refresh_res)
        sql_cmd = f"drop subscription if exists {self.subname};"
        drop_sub_res = self.commsh_sub.execut_db_sql(
            sql_cmd, sql_type=self.user_param_sub)
        self.log.info(drop_sub_res)
        sql_cmd = f"drop publication if exists {self.pubname};"
        drop_pub_res = self.commsh_pub.execut_db_sql(
            sql_cmd, self.user_param_pub, None, macro.DB_ENV_PATH_REMOTE1)
        self.log.info(drop_pub_res)
        sql_cmd = f"drop table if exists {self.table1} cascade;" \
                  f"drop table if exists {self.table2} cascade;"
        sub_drop_res = self.commsh_sub.execut_db_sql(
            sql_cmd, sql_type=self.user_param_sub)
        self.log.info(sub_drop_res)
        pub_drop_res = self.commsh_pub.execut_db_sql(
            sql_cmd, self.user_param_pub, None, macro.DB_ENV_PATH_REMOTE1)
        self.log.info(pub_drop_res)
        guc_res1 = self.commsh_pub.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG, '',
            'all', False, False, macro.DB_INSTANCE_PATH_REMOTE1,
            f'host replication {self.pri_userdb_sub.db_user} '
            f'{self.pri_userdb_sub.db_host}/32',
            macro.DB_ENV_PATH_REMOTE1)
        self.log.info(guc_res1)
        guc_res2 = self.commsh_pub.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG, '',
            'all', False, False, macro.DB_INSTANCE_PATH_REMOTE1,
            f'host replication {self.std_userdb_sub1.db_user} '
            f'{self.std_userdb_sub1.db_host}/32',
            macro.DB_ENV_PATH_REMOTE1)
        self.log.info(guc_res2)
        guc_res3 = self.commsh_sub.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG, '',
            'all', False, False, '',
            f'host replication {self.pri_userdb_pub.db_user} '
            f'{self.pri_userdb_pub.db_host}/32')
        self.log.info(guc_res3)
        res_wal = self.commsh_pub.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG,
            f'wal_level={self.wal_level}',
            dn_path=macro.DB_INSTANCE_PATH_REMOTE1,
            env_path=macro.DB_ENV_PATH_REMOTE1)
        self.log.info(res_wal)
        res_slot = self.commsh_sub.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG,
            f'enable_slot_log={self.enable_slot_log}')
        self.log.info(res_slot)
        pub_restartdb = self.commsh_pub.restart_db_cluster(
            env_path=macro.DB_ENV_PATH_REMOTE1)
        self.assertTrue(pub_restartdb, f'发布端重启数据库失败:{text}')
        self.assertTrue(sub_switch_res, f'发布端主备切换失败:{text}')
        self.assertTrue(sub_refresh_res, f'refreshconf失败:{text}')
        self.assertIn(self.constant.drop_sub_succ_msg, drop_sub_res,
                      f'删除订阅失败:{text}')
        self.assertIn(self.constant.drop_pub_succ_msg, drop_pub_res,
                      f'删除发布失败:{text}')
        self.assertEqual(sub_drop_res.count(self.constant.DROP_TABLE_SUCCESS),
                         2, f'发布端删除表失败:{text}')
        self.assertEqual(pub_drop_res.count(self.constant.DROP_TABLE_SUCCESS),
                         2, f'订阅端删除表失败:{text}')
        self.assertTrue(guc_res1 and guc_res2, f'发布端删除白名单失败:{text}')
        self.assertTrue(guc_res3, f'订阅端删除白名单失败:{text}')
        self.assertTrue(res_wal, f'发布端恢复wal_level参数失败:{text}')
        self.assertTrue(res_slot, f'订阅端恢复enable_slot_log失败:{text}')
        self.log.info(f"-----{os.path.basename(__file__)} end-----")
